﻿#ifndef __SS_WSPP_NETWORK_H__
#define __SS_WSPP_NETWORK_H__
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include "ssWsppBase.h"

using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;

namespace StarSeeker
{

typedef websocketpp::server<websocketpp::config::asio> server;
typedef server::message_ptr message_ptr;
typedef websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread_ptr;

//网络线程
class ssWsppNetwork : public ssWsppBase
{
public:
	ssWsppNetwork();
	~ssWsppNetwork();
	//启动网络服务
	bool StartNetWork(const char* ip, const u16 port, bool isV6);
	//停止网络服务
	void StopNetWork();
	//关闭连接
	void DoCloseSocket(u32 sessionId);
	//发送数据
	void DoSendData(u32 sessionId, const char* buf, u32 len);
	void DoSendDataMulti(std::vector<u32>* sessionId, const char* buf, u32 len);
private:
	//处理连接关闭
	void HandleClose(u32 sessionId);
	//处理发送
	void HandleSend(u32 sessionId, const char* buf, u32 len);
	void HandleSendMulti(std::vector<u32>* sessionId, const char* buf, u32 len);
	//删除session对象
	void DeleteSession(websocketpp::connection_hdl hdl);

	//websocketpp的回调函数
	void OnMessage(server* s, websocketpp::connection_hdl hdl, message_ptr msg);
	//void OnHttp(server* s, websocketpp::connection_hdl hdl);
	void OnSocketInit(websocketpp::connection_hdl hdl, asio::ip::tcp::socket & s);
	void OnFail(websocketpp::connection_hdl hdl);
	void OnClose(websocketpp::connection_hdl hdl);
	void OnOpen(websocketpp::connection_hdl hdl);
	//void OnInterrupt(websocketpp::connection_hdl hdl);


	asio::ip::tcp::endpoint		m_addr;
	server						m_echoServer;
};

}
#endif
